import logging
import os

from common.config import config
from common.utils import to_abs_path


def setup_logger(name, log_file=to_abs_path(config.LOG_FILE)):
    os.makedirs(os.path.dirname(log_file), exist_ok=True)
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s'
    )
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)
    console_handler.setFormatter(formatter)

    file_handler = logging.FileHandler(log_file, mode='a', encoding="utf-8")
    file_handler.setLevel(logging.DEBUG)
    file_handler.setFormatter(formatter)

    _logger = logging.getLogger(name)
    _logger.setLevel(logging.DEBUG)
    _logger.addHandler(console_handler)
    _logger.addHandler(file_handler)
    return _logger


logger = setup_logger(config.APP_NAME)
logger.info('logger initialize completed')
